// pages/shopping/editAddress.js
import {
    district,
    user
} from '../../api/apiData'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        autosize: {
            minHeight: 40
        },
        checked: true,
        districtList: [],
        district_show: false,
        district: '', //籍贯
        district_id: '',
        name: '',
        phone: '',
        detail: '',
        type: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if (options.type == 1) {
            this.setData({
                type: options.type
            })
            wx.setNavigationBarTitle({
                title: '编辑',
            })
            user.address.detail({
                address_id: options.id
            }, res => {
                if (res.code == 1) {
                    this.setData({
                        address_id: res.data.id,
                        name: res.data.name,
                        phone: res.data.phone,
                        district: res.data.district.merger_name,
                        detail: res.data.detail,
                        is_default: res.data.is_default,
                        checked: res.data.is_default == 1 ? true : false
                    })
                }

            })
        }
        this.getDistrict()
    },
    onChange(event) {
        this.setData({
            checked: event.detail,
        });
    },
    // 获取地区
    getDistrict() {
        district.treeList({}, res => {
            console.log(res)
            let city = res.data
            let columns = [{
                    values: city,
                    className: 'column1',
                },
                {
                    values: city[0].children,
                    className: 'column2'
                },
                {
                    values: city[0].children[0].children,
                    className: 'column3'
                }
            ]
            this.setData({
                districtList: columns
            })
        })
    },
    //弹出框，获取改变值
    handlePickChange(event) {
        const {
            picker,
            value,
            index
        } = event.detail;
        console.log(value[index].children)
        if (index == 0) {
            picker.setColumnValues(index + 1, value[index].children);
            picker.setColumnValues(index + 2, value[index].children[0].children);
        } else if (index == 1) {
            picker.setColumnValues(index + 1, value[index].children);
        } else {
            // picker.setColumnValues(index, value[index - 1].children);
        }
    },
    onConfirmDistrict(e) {
        console.log(e)
        let value = e.detail.value;
        console.log(value)
        let text = value[0].name + '/' + value[1].name + '/' + value[2].name
        console.log(text)
        this.setData({
            district: text,
            district_show: false,
            district_id: value[2].id,
        })
    },
    closeDistrict() {
        this.setData({
            district_show: false
        })
    },
    toShow() {
        this.setData({
            district_show: true
        })
    },
    toSubmit() {
        let obj = {
            name: this.data.name,
            phone: this.data.phone,
            district_id: this.data.district_id,
            detail: this.data.detail,
            is_default: this.data.checked ? 1 : 0,
        }
        console.log(obj)
        if (this.data.type == 1) {
            obj.address_id = this.data.address_id
            user.address.edit(obj, res => {
                if (res.code == 1) {
                    wx.showToast({
                        title: res.msg,
                    })
                    setTimeout(() => {
                        wx.navigateBack()
                    }, 1000)
                } else {
                    wx.showToast({
                        title: res.msg,
                        icon: 'error'
                    })
                }
            })
        } else {
            user.address.add(obj, res => {
                if (res.code == 1) {
                    wx.showToast({
                        title: res.msg,
                    })
                    setTimeout(() => {
                        wx.navigateBack()
                    }, 1000)
                } else {
                    wx.showToast({
                        title: res.msg,
                        icon: 'error'
                    })
                }
            })
        }

    },
    // 删除
    toDel() {
        let that = this
        wx.showModal({
            title: '提示',
            content: '确定要删除吗？',
            showCancel: true,//是否显示取消按钮
            cancelText:"否",//默认是“取消”
            // cancelColor:'skyblue',//取消文字的颜色
            confirmText:"是",//默认是“确定”
            // confirmColor: 'skyblue',//确定文字的颜色
            success: function (res) {
               if (res.cancel) {
                  //点击取消,默认隐藏弹框
                 
               } else {
                  //点击确定
                  user.address.delete({
                    address_id: that.data.address_id
                }, res => {
                    if (res.code == 1) {
                        wx.showToast({
                            title: res.msg,
                        })
                        setTimeout(() => {
                            wx.navigateBack()
                        }, 1000)
                    } else {
                        wx.showToast({
                            title: res.msg,
                            icon: 'error'
                        })
                    }
        
                })
               }
            },
            fail: function (res) { },//接口调用失败的回调函数
            complete: function (res) { },//接口调用结束的回调函数（调用成功、失败都会执行）
         })
       
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})